<template>
  <div class="app-container">
    <!-- 按钮组 -->
    <el-row style="padding-top:10px;">
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-button-group>
            <el-button type="primary" @click="addClk">新增</el-button>
            <el-button type="danger" @click="deleteAllClk">多选删除</el-button>
          </el-button-group>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="grid-content bg-purple-light">
          <div>
            <el-input placeholder="请输入内容" @blur="searchBlur" v-model="searchVal" class="input-with-select">
              <el-button slot="append" @click="searchClk" icon="el-icon-search"></el-button>
            </el-input>
          </div>
        </div>
      </el-col>
    </el-row>
    <!-- 表格 -->
    <template>
      <!-- @sort-change="sortChange" -->
      <el-table
        max-height="400"
        @selection-change="selectChange"
        :data="listArr"
        border
        style="width: 100%;margin:10px auto;"
        id="table"
      >
        <el-table-column fixed type="selection" width="50"></el-table-column>
        <el-table-column fixed type="index" label="#" width="50"></el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column prop="content" label="内容" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="type" label="类别" width="80"></el-table-column>
        <el-table-column prop="author" label="作者" width="80"></el-table-column>
        <el-table-column prop="time" label="时间" sortable width="80"></el-table-column>
        <el-table-column prop="date" label="日期" sortable></el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button @click="updateClk(scope.row)" type="text" size="small">修改</el-button>
            <el-button type="text" size="small" @click="deleteClk(scope.row.id)">删除</el-button>
            <el-button type="text" size="small" @click="lookClk(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!-- 抽屉 -->
    <el-drawer
      :title="title"
      size="50%"
      :visible.sync="drawer"
      :before-close="handleClose"
      :with-header="true"
    >
      <!-- form -->
      <el-form ref="form" size="mini" style="padding:0 5%;" :model="form" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="form.title" :disabled="look"></el-input>
        </el-form-item>
        <el-form-item label="类别">
          <el-select v-model="form.cid" :disabled="look" placeholder="请选择类别">
            <el-option
              v-for="(item,index) in classifys"
              :key="index"
              :value="item.id"
              :label="item.name"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用时">
          <el-select :disabled="look" v-model="form.time" placeholder="请选择用时">
            <el-option label="一分钟" value="1"></el-option>
            <el-option label="二分钟" value="2"></el-option>
            <el-option label="三分钟" value="3"></el-option>
            <el-option label="四分钟" value="4"></el-option>
            <el-option label="五分钟" value="5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作者">
          <el-input :disabled="true" v-model="form.author"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <div class="zzc" v-show="look"></div>
          <mavonEditor
            :disabled="look"
            v-model="form.content"
            style="width:98%;max-height:200px;overflow-y: auto;"
          ></mavonEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :disabled="look" @click="onSubmit">确定</el-button>
          <el-button @click="close">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[3, 5, 8, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import { mapActions, mapState, mapMutations, mapGetters } from "vuex";
import { mavonEditor } from "mavon-editor";
import "mavon-editor/dist/css/index.css";
export default {
  data() {
    return {
      drawer: false,
      form: {
        author: "situ"
      },
      title: "新增",
      idArr: [],
      searchVal: "",
      look: false
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    ...mapActions("msbd", ["getList", "addlist", "updatelist", "deletelist"]),
    ...mapMutations("msbd", ["SET_CURR", "SET_PAGE", "SET_SEARCH"]),  // "SET_SORT"
    lookClk(row) {
      this.look = true;
      this.title = "查看";
      this.drawer = true;
      this.form = { ...row };
    },
    // sortChange(val) {
    //   this.SET_SORT(val);
    //   this.getList();
    // },
    searchBlur(){
      this.searchClk()
    },
    searchClk() {
      this.SET_SEARCH(this.searchVal);
      this.getList();
    },
    deleteAllClk() {
      if (!this.idArr.length) {
        this.$message({
          message: "请至少选择一项进行删除",
          type: "warning"
        });
        return;
      }
      let arr = this.idArr.map(v => {
        return v.id;
      });
      this.deleteClk(arr.join(","));
    },
    selectChange(val) {
      this.idArr = val;
    },
    deleteClk(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deletelist(id);
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    updateClk(row) {
      this.look = false;
      this.form = { ...row };
      this.title = "修改";
      this.drawer = true;
    },
    addClk() {
      this.look = false;
      this.title = "新增";
      this.drawer = true;
    },
    close() {
      this.$confirm("确认关闭？")
        .then(_ => {
          this.drawer = false;
          this.form = {
            author: "situ"
          };
        })
        .catch(_ => {});
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    onSubmit() {
      if (this.form.id) {
        this.updatelist(this.form);
      } else {
        this.addlist(this.form);
      }
      this.drawer = false;
      this.form = {
        author: "situ"
      };
    },
    handleSizeChange(val) {
      this.SET_PAGE(val);
      this.getList();
    },
    handleCurrentChange(val) {
      this.SET_CURR(val);
      this.getList();
    }
  },
  computed: {
    ...mapState("msbd", [
      "listArr",
      "classify",
      "total",
      "currentPage",
      "pageSize"
    ]),
    ...mapGetters(["classifys"])
  },
  components: {
    mavonEditor
  }
};
</script>

<style scoped>
.zzc {
  z-index: 9999;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
}

/* #table {
  height: calc(100vh - 230px);
} */
</style>